home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / UniqueKey_oracle / MasterDetail.m < prev    next >
Text File  |  1994-06-14  |  8KB  |  190 lines

  1. #import "UniqueKey.h"
  2. #import "MasterDetail.h"
  3.  
  4.  
  5. static BOOL _Debug = YES;
  6.  
  7. @implementation MasterDetail
  8.  
  9. /******************************************************************************
  10. * Create instance of the UniqueKey object to grab blocks of <count> keys for
  11. * our use during insert.  The count is set low so you can see the SQL reserve
  12. * blocks of keys.  Up the count to something larger for it to be of use.
  13. *
  14. * The login information (connection dictionary) has been blanked out, since
  15. * you may elect to have the tables in some database other than PEOPLE, so we
  16. * elect to run a login panel.  Once the login is complete we can get the
  17. * connection dictionary from the adaptor and use that information to allow
  18. * UniqueKey to login and create its separate connection for key reservation.
  19. *
  20. * Many of the method calls in EOF return (id)<some protocol>, so you will notice
  21. * several casts to (id) to avoid warnings for nested method calls supported by
  22. * the returned object, but not by the protocol.  Alternatively, you can cast to
  23. * the exact class you know returned, such as (EODatabaseDataSource*) or
  24. * (EODetailDatabaseSource*).
  25. ******************************************************************************/
  26. - appDidInit:sender
  27. {
  28.     EOAdaptorChannel *eoAdaptorChannel = [[(id)[employeeController dataSource] databaseChannel] adaptorChannel];
  29.     EOAdaptor        *eoAdaptor        = [[eoAdaptorChannel adaptorContext] adaptor];
  30.     
  31.     if(_Debug) [eoAdaptorChannel setDelegate:self];
  32.  
  33.     employeeEntity       = [[(id)[employeeController dataSource] entity] retain];
  34.     equipmentOwnerEntity = [[(id)[equipmentOwnerController dataSource] entity] retain];
  35.  
  36.     if(![eoAdaptor runLoginPanelAndValidateConnectionDictionary]) [NXApp terminate:self];
  37.     [UniqueKey setConnectionDictionary:[eoAdaptor connectionDictionary]];
  38.     
  39.     employeeUniqueKey = [[[UniqueKey alloc] initWithEntity:employeeEntity count:5] retain];
  40.     if(!employeeUniqueKey) [NXApp terminate:self];
  41.  
  42.     [self setFetchOrderFor:employeeController with:@"LastName" order:EOAscendingOrder];
  43.     [self setFetchOrderFor:equipmentOwnerController with:@"Description" order:EOAscendingOrder];
  44.  
  45.     [employeeController fetch:self];
  46.     return self;
  47. }
  48.  
  49.  
  50. /******************************************************************************
  51. * Set a controller's data source to fetch sorted by a given attribute name
  52. * and order.
  53. ******************************************************************************/
  54. - setFetchOrderFor:(EOController*)controller with:(NSString*)attributeName order:(EOOrdering)order
  55. {
  56.     id        dataSource = [controller dataSource];
  57.     id        attribute  = [[dataSource entity] attributeNamed:attributeName];
  58.     NSArray    *orderArray;
  59.     
  60.     orderArray = [NSArray arrayWithObject:
  61.         [EOAttributeOrdering attributeOrderingWithAttribute:attribute ordering:order]];
  62.     [dataSource setFetchOrder:orderArray];
  63.     return self;
  64. }
  65.  
  66.  
  67. /******************************************************************************
  68. * Generate unique keys for the new object.  Use the instance of UniqueKey
  69. * to dole out a key from its internal buffer.  The UniqueKey object has its
  70. * own channel to the DB which it uses to allocate blocks of keys.
  71. ******************************************************************************/
  72. - (BOOL)controller:controller willInsertObject:object;
  73. {
  74.     if(controller==employeeController)
  75.     {
  76.     NSNumber       *uniqueKey  = [NSNumber numberWithInt:[employeeUniqueKey nextKey]];
  77.     NSArray        *emptyArray = [[[NSArray alloc] init] autorelease];
  78.  
  79.     [object setObject:uniqueKey      forKey:@"EmpId"];
  80.     [object setObject:@"<LastName>"  forKey:@"LastName"];
  81.     [object setObject:@"<FirstName>" forKey:@"FirstName"];
  82.     [object setObject:@"<Phone>"     forKey:@"Phone"];
  83.     [object setObject:emptyArray     forKey:@"toEmpEquipment"];
  84.     return YES;
  85.     }
  86.     return NO;
  87. }
  88.  
  89.  
  90. /******************************************************************************
  91. * When an employee is deleted from the database, we need to remove that person's
  92. * ownership of their equipment.  We use the toEmpEquipment relationship to get
  93. * an NSArray of equipment objects and null the EmpId for each.
  94. ******************************************************************************/
  95. - (BOOL)controller:controller willDeleteObject:object
  96. {
  97.     if(controller==employeeController) {
  98.         NSArray     *eeArray      = [object objectForKey:@"toEmpEquipment"];
  99.     NSEnumerator    *eeEnumerator = [eeArray objectEnumerator];
  100.     EOGenericRecord    *equipment;
  101.     
  102.     while(equipment = [eeEnumerator nextObject]) {
  103.         [equipment setObject:[EONull null] forKey:@"EmpId"];
  104.         [(id)[employeeController dataSource] updateObject:equipment];
  105.     }
  106.     }
  107.     return YES;
  108. }
  109.  
  110.  
  111. /******************************************************************************
  112. * Bring up the assign equipment panel and start a modal session.  Construct 
  113. * an 'otherEquipment' qualifier to select equipment not currently assigned to
  114. * the selected employee.
  115. *
  116. * The Beta release of the Oracle adapter has a problem with outer joins, so the
  117. * model for our Oracle example was changed to a regular join.  This will prevent
  118. * you from seeing unassigned (EmpId=NULL) equipment in the assign equipment
  119. * panel.
  120. ******************************************************************************/
  121. - assignEquipmentToEmployee:sender
  122. {
  123.     EOGenericRecord    *employee = [(id)[equipmentForEmployeeController dataSource] masterObject];
  124.     NSNumber        *empId    = [employee objectForKey:@"EmpId"];
  125.     EOQualifier        *otherEquipment;
  126.     NSString        *qualifierString;
  127.     
  128.     qualifierString = [NSString stringWithFormat:@"EmpId != %@",empId];
  129.     
  130.     otherEquipment = [[[EOQualifier alloc]
  131.          initWithEntity:equipmentOwnerEntity qualifierFormat:qualifierString] autorelease];
  132.     
  133.     [(id)[equipmentOwnerController dataSource] setQualifier:otherEquipment];
  134.     [equipmentOwnerController clearSelection];
  135.     [equipmentOwnerController fetch];
  136.     [[assignEquipmentPanel center] makeKeyAndOrderFront:nil];
  137.     [NXApp runModalFor:assignEquipmentPanel];
  138.     [assignEquipmentPanel orderOut:self];
  139.     [employeeController fetch];
  140.     return self;
  141. }
  142.  
  143. - assignEquipmentToEmployeeOK:sender
  144. {
  145.     EOGenericRecord    *employee       = [(id)[equipmentForEmployeeController dataSource] masterObject];
  146.     NSNumber        *empId          = [employee objectForKey:@"EmpId"];
  147.     NSArray        *eoArray    = [equipmentOwnerController selectedObjects];
  148.     NSEnumerator    *eoEnumerator    = [eoArray objectEnumerator];
  149.     EOGenericRecord    *equipmentOwner;
  150.  
  151.     while(equipmentOwner = [eoEnumerator nextObject]) {
  152.     [equipmentOwner setObject:empId forKey:@"EmpId"];
  153.     [(id)[equipmentOwnerController dataSource] updateObject:equipmentOwner];
  154.     }
  155.     [NXApp stopModal];
  156.     return self;
  157. }
  158.  
  159.  
  160. /******************************************************************************
  161. * Release the equipment shown in the detail view.  Null out the equipment owner's
  162. * EmpId for each.
  163. ******************************************************************************/
  164. - releaseEquipmentForEmployee:sender
  165. {
  166.     NSArray        *efeArray    = [equipmentForEmployeeController selectedObjects];
  167.     NSEnumerator    *efeEnumerator    = [efeArray objectEnumerator];
  168.     EOGenericRecord    *equipment;
  169.  
  170.     while(equipment = [efeEnumerator nextObject]) {
  171.     [equipment setObject:[EONull null] forKey:@"EmpId"];
  172.     [(id)[equipmentForEmployeeController dataSource] updateObject:equipment];
  173.     }
  174.     [employeeController fetch];
  175.     return self;
  176. }
  177.  
  178.  
  179. /******************************************************************************
  180. * Echo SQL when debug is enabled.
  181. ******************************************************************************/
  182. - (EODelegateResponse)adaptorChannel:channel willEvaluateExpression:(NSMutableString *)expression
  183. {
  184.     NSLog(expression);
  185.     return EODelegateApproves;
  186. }
  187.  
  188.  
  189. @end
  190.